package xframe.util.db;

import java.sql.*;
import xframe.init.StaticValue;


public class DBConnector{
	 static Connection con;

    public DBConnector(){    }

    /*public static Connection getConnection()throws NamingException, SQLException, XMLQueryException{
        Connection conn = null;
        String dataSourceName = DynamicVar.get("xframe.datasource");
        try
        {
            InitialContext ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup(dataSourceName);
            conn = ds.getConnection();
        }
        catch(NamingException e)
        {
            throw new NamingException("Can not Search the NamingService");
        }
        catch(SQLException se)
        {
            throw new SQLException("Can not the Database Connection");
        }
        return conn;
    }
*/
    
    
    public static Connection getConnection() throws ClassNotFoundException, SQLException {

              String url = StaticValue.get("db.url");
              String user = StaticValue.get("db.user");
              String pass = StaticValue.get("db.pass");
              Class.forName(StaticValue.get("db.driver"));
              con=DriverManager.getConnection(url,user,pass);
          return con;
      }
    
    public static void release(Connection con)throws SQLException{
        if(con != null)
            try{
                con.close();
            }catch(SQLException e){
                throw new SQLException("Failed in Closing Database *Connection ");
            }
    }

    public static void release(Connection con, Statement stmt, ResultSet rs)
        throws SQLException
    {
        if(rs != null)
            try
            {
                rs.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *ResultSet ");
            }
        if(stmt != null)
            try
            {
                stmt.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *Statement ");
            }
        if(con != null)
            try
            {
                con.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *Connection ");
            }
    }

    public static void release(Connection con, PreparedStatement psmt, ResultSet rs)
        throws SQLException
    {
        if(rs != null)
            try
            {
                rs.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *ResultSet ");
            }
        if(psmt != null)
            try
            {
                psmt.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *PreparedStatement ");
            }
        if(con != null)
            try
            {
                con.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *Connection ");
            }
    }

    public static void release(PreparedStatement psmt, ResultSet rs)
        throws SQLException
    {
        if(rs != null)
            try
            {
                rs.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *ResultSet ");
            }
        if(psmt != null)
            try
            {
                psmt.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *PreparedStatement ");
            }
    }

    public static void release(Statement stmt, ResultSet rs)
        throws SQLException
    {
        if(rs != null)
            try
            {
                rs.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *ResultSet ");
            }
        if(stmt != null)
            try
            {
                stmt.close();
            }
            catch(SQLException e)
            {
                throw new SQLException("Failed in Closing Database *PreparedStatement ");
            }
    }

   
}
